<template>
    <el-card class="w-full pt-[5px]">
        <el-radio-group v-model="searchForm.reviewPlanStatus" @change="getList(1)">
            <el-radio-button v-for="item in statisticsList" :key="item.value" :value="item.value">{{ item.name }}({{
            item.count
        }})</el-radio-button>
        </el-radio-group>
        <el-form :model="searchForm" class="mt-[20px]" label-width="110px">
            <el-row :gutter="20">
                <el-col :span="5">
                    <el-form-item label="评审计划名称" class="mb-[0px]">
                        <el-input v-model="searchForm.name" clearable />
                    </el-form-item>
                </el-col>
                <el-col :span="5">
                    <el-form-item label="评审方案" class="mb-[0px]">
                        <!-- <el-select v-model="searchForm.reviewSchemeId" multiple collapse-tags collapse-tags-tooltip
                            clearable>
                            <el-option v-for="item in fatableData" :key="item.id" :label="item.name" :value="item.id" />
                        </el-select> -->
                        <CustomTreeSelect placeholder="评审方案" v-model="searchForm.reviewSchemeId" valueKey="id"
                            labelKey="name" :isFetch="false" :dataList="fatableData" />
                    </el-form-item>
                </el-col>
                <el-col :span="6">
                    <el-form-item label="开始时间">
                        <el-date-picker v-model="valueDate1" format="YYYY/MM/DD" value-format="YYYY-MM-DD"
                            type="daterange" range-separator="至" />
                    </el-form-item>
                </el-col>
                <el-col :span="6">
                    <el-form-item label="结束时间">
                        <el-date-picker v-model="valueDate2" format="YYYY/MM/DD" value-format="YYYY-MM-DD"
                            type="daterange" range-separator="至" />
                    </el-form-item>
                </el-col>


                <el-col :span="2">
                    <div class="flex justify-end">
                        <el-button type="primary" @click="queryMet">查询</el-button>
                    </div>
                </el-col>
            </el-row>
        </el-form>
    </el-card>
    <el-card class="w-full pt-[5px] mt-[10px]">
        <div class="mb-[20px]">
            <!-- <el-button type="primary" @click="handleAdd">
                <el-icon class="mr-[5px]">
                    <Plus />
                </el-icon>
                新增
            </el-button> -->
            <el-button type="primary" @click="dialogVisible = true">
                评审方案
            </el-button>
            <el-button @click="handleDel">
                <el-icon>
                    <DeleteFilled />
                </el-icon>
                删除
            </el-button>
        </div>
        <el-table :data="tableData" class="w-full" @selection-change="handleSelectionChange" stripe>
            <el-table-column type="selection" width="55" />
            <el-table-column prop="name" label="评审计划名称">
                <template #default="scope">
                    <el-link type="primary" @click="viewItem(scope.row)">{{ scope.row.name }}</el-link>
                </template>
            </el-table-column>
            <el-table-column prop="reviewSchemeId" label="评审方案" :formatter="formatpsfaId" />
            <el-table-column prop="beginDate" label="开始时间" />
            <el-table-column prop="reviewHonorApplyBookNum" label="评审成果数" />
            <el-table-column prop="endDate" label="结束时间" />
            <el-table-column prop="reviewExpertNum" label="评审专家数（人）" />
            <el-table-column prop="reviewPlanStatus" label="评审状态" :formatter="formatpsztId" />
            <el-table-column fixed="right" label="操作" width="380" align="center">
                <template #default="scope">
                    <el-button type="primary" size="small" plain @click="handleEdit(scope.row)">
                        编辑
                    </el-button>
                    <el-button type="primary" size="small" plain
                        v-if="scope.row.reviewPlanStatus == 'stop' || scope.row.reviewPlanStatus == 'notStart' || scope.row.reviewPlanStatus == null"
                        @click="updateStatusById('review', scope.row.id)">
                        启动
                    </el-button>
                    <el-button type="primary" size="small" plain v-if="scope.row.reviewPlanStatus == 'review'"
                        @click="updateStatusById('stop', scope.row.id)">
                        结束
                    </el-button>
                    <el-button type="primary" size="small" plain
                        v-if="scope.row.reviewPlanStatus == 'review' || scope.row.reviewPlanStatus == 'stop'"
                        @click="gockhj(scope.row)">查看获奖</el-button>
                    <el-button type="primary" size="small"
                        v-if="scope.row.reviewPlanStatus == 'review' || scope.row.reviewPlanStatus == 'stop'"
                        @click="gopszj(scope.row)" plain>查看专家</el-button>
                    <el-button type="primary" size="small" plain @click="delHonor(scope.row)">删除</el-button>
                </template>
            </el-table-column>
        </el-table>
        <div class="mt-[20px] flex justify-end">
            <pagination v-show="total > 0" :total="total" v-model:page="pageInfo.pageNum"
                v-model:limit="pageInfo.pageSize" @pagination="getList" />
        </div>
    </el-card>
    <EditModal v-model="editModelVisible" ref="EditModalRef" :fatableData="fatableData" @callBack="callBackFun" />
    <viewModal v-model="ViewdialogVisible" ref="viewModalRef" :fatableData="fatableData" />
    <el-dialog v-model="dialogVisible" title="评审方案" width="80%" align-center destroy-on-close draggable
        :before-close="handleClose">
        <psfa></psfa>
    </el-dialog>
</template>

<script setup>
import { onMounted } from 'vue';
import { getCategory } from "@/api/system/category"
import { useRouter } from "vue-router";
import { ElMessage, ElMessageBox } from "element-plus";
import { getreviewPlanList, reviewPlanDel, getreviewPlanInfo, getreviewSchemeList, getreviewPlanUpdateStatusById, getreviewPlanListStatistics } from "@/api/achievement/cghj"
import psfa from './psfa.vue'
import EditModal from "./EditModal.vue";
import viewModal from "./viewModal.vue";
import CustomTreeSelect from "@/views/project/components/CustomTreeSelect.vue";


const router = useRouter();
const editModelVisible = ref(false);
const ViewdialogVisible = ref(false);
const dialogVisible = ref(false);
const searchForm = ref({
    name: "",
    reviewPlanStatus: "",
    beginDateStart: "",
    beginDateEnd: "",
    endDateStart: "",
    endDateEnd: "",
    reviewSchemeId: [],
});
const pageInfo = ref({
    pageNum: 1,
    pageSize: 10
})
const total = ref(0)
const tableData = ref([]);
//点击新增
const handleAdd = () => {
    editModelVisible.value = true;
};
const EditModalRef = ref(null)
//点击编辑
const handleEdit = (i) => {
    getreviewPlanInfo(i.id).then(res => {
        if (res.code == 200) {
            editModelVisible.value = true;
            EditModalRef.value.getRuleForm(res.data)
        }
    })
};


const viewModalRef = ref(null)
const viewItem = (i) => {
    getreviewPlanInfo(i.id).then(res => {
        if (res.code == 200) {
            ViewdialogVisible.value = true;
            viewModalRef.value.getRuleForm(res.data)
        }
    })
};
const multipleSelection = ref([])
const handleSelectionChange = (val) => {
    multipleSelection.value = val
}
//点击删除
const handleDel = () => {
    if (multipleSelection.value.length == 0) {
        ElMessage.warning("请选择要删除的数据")
        return;

    } else {
        ElMessageBox.confirm("是否确认删除？", "提示", {
            type: "warning",
        }).then(() => {
            let ids = multipleSelection.value.map(item => item.id)
            ids = ids.join(',')
            reviewPlanDel(ids).then(res => {
                if (res.code == 200) {
                    getList()
                    ElMessage.success('删除成功')
                }
            })
        });
    }

};
const psztList = ref([{
    code: "stop",
    name: '已停止'
}, {
    code: "review",
    name: '评审中'
}, {
    code: "notStart",
    name: '未启动'
}])
const formatpsztId = (row, column, cellValue, index) => {
    console.log(cellValue)
    console.log(psztList.value)
    if (psztList.value.length && cellValue) {
        return psztList.value.find(item => item.code == cellValue) ? psztList.value.find(item => item.code == cellValue).name : ''
    } else {
        return ''
    }
}
const formatpsfaId = (row, column, cellValue, index) => {

    if (fatableData.value.length && cellValue) {
        return fatableData.value.find(item => item.id == cellValue) ? fatableData.value.find(item => item.id == cellValue).name : ''
    } else {
        return ''
    }
}
const getCategoryMet = async () => {
    let parms = {
        bizName: "评审状态"
    }
    let res = await getCategory(parms)
    if (res.code == 200) {
        psztList.value = res.data
    }
}
const initList = () => {
    // Promise.all([getCategoryMet()]).then(() => {

    // })
    getList()
}
const valueDate1 = ref('')
const valueDate2 = ref('')
const fatableData = ref([])
const getList = async (i) => {
    getreviewPlanListStatisticsMet()
    if (i == 1) {
        pageInfo.value.pageNum = 1
    }
    if (valueDate1.value && valueDate1.value.length) {
        searchForm.value.beginDateStart = valueDate1.value[0]
        searchForm.value.beginDateEnd = valueDate1.value[1]
    } else {
        searchForm.value.beginDateStart = ''
        searchForm.value.beginDateEnd = ''
    }
    if (valueDate2.value && valueDate2.value.length) {
        searchForm.value.endDateStart = valueDate2.value[0]
        searchForm.value.endDateEnd = valueDate2.value[1]
    } else {
        searchForm.value.endDateStart = ''
        searchForm.value.endDateEnd = ''
    }
    let parms = {
        ...searchForm.value,
        ...pageInfo.value
    }
    parms.reviewSchemeId = searchForm.value.reviewSchemeId.join(',')
    let res = await getreviewPlanList(parms)
    if (res.code == 200) {
        tableData.value = res.rows
        total.value = res.total
        console.log(tableData.value)

    }
}
const getfaList = async () => {

    let parms = {

        pageNum: 1,
        pageSize: 100
    }
    let res = await getreviewSchemeList(parms)
    if (res.code == 200) {
        fatableData.value = res.rows
    }
}
const handleClose = (done) => {
    getfaList()
    done()
}
const callBackFun = () => {
    getList()
}
const delHonor = (i) => {
    ElMessageBox.confirm(
        '确认！删除这条数据?',
        '提醒',
        {
            confirmButtonText: '确定',
            cancelButtonText: '取消',
            type: 'warning',
        }
    )
        .then(() => {
            reviewPlanDel(i.id).then(res => {
                if (res.code == 200) {
                    getList()
                    ElMessage.success('删除成功')
                }
            })


        })
        .catch(() => {

        })

}

const updateStatusById = (i, id) => {
    let parms = {
        id: id,
        reviewPlanStatus: i
    }
    getreviewPlanUpdateStatusById(parms).then(res => {
        if (res.code == 200) {
            ElMessage.success('操作成功')
            getList()
        }

    })
}
const gockhj = (i) => {
    router.push({
        path: '/achievement/cghj/ckhj/index',
        query: { id: i.id, reviewSchemeId: i.reviewSchemeId, applyInfoId: i.applyInfoId, reviewPlanStatus: i.reviewPlanStatus }

    })
}

const gopszj = (i) => {
    router.push({
        path: '/achievement/cghj/pszj/index',
        query: { id: i.id, applyInfoId: i.applyInfoId }
    })
}


const statisticsList = ref([{
    name: "全部",
    value: '',
    count: 0
},
{
    name: "未启动",
    value: 'notStart',
    count: 0
},
{
    name: "评审中",
    value: 'review',
    count: 0
},
{
    name: "已停止",
    value: 'stop',
    count: 0
}])

const getreviewPlanListStatisticsMet = async () => {
    if (valueDate1.value && valueDate1.value.length) {
        searchForm.value.beginDateStart = valueDate1.value[0]
        searchForm.value.beginDateEnd = valueDate1.value[1]
    } else {
        searchForm.value.beginDateStart = ''
        searchForm.value.beginDateEnd = ''
    }
    if (valueDate2.value && valueDate2.value.length) {
        searchForm.value.endDateStart = valueDate2.value[0]
        searchForm.value.endDateEnd = valueDate2.value[1]
    } else {
        searchForm.value.endDateStart = ''
        searchForm.value.endDateEnd = ''
    }
    let parms = {
        ...searchForm.value,
    }
    parms.reviewSchemeId = searchForm.value.reviewSchemeId.join(',')
    delete parms.reviewPlanStatus
    let res = await getreviewPlanListStatistics(parms);
    if (res.code == 200) {
        statisticsList.value.forEach(item => {
            if (item.name != '全部') {
                let hasStatis = res.data.filter(item1 => {
                    return item1.reviewPlanStatus == item.value
                })
                hasStatis && hasStatis.length ? item.count = hasStatis[0].count : item.count = 0
            }


        })
        let allNum = 0
        res.data.forEach(item => {
            allNum += item.count
        })
        statisticsList.value[0].count = allNum
    }
};
const queryMet = () => {
    getList(1)
}

onMounted(() => {
    initList()
    getfaList()
});
</script>

<style scoped lang="scss">
@import url("@/views/achievement/index.scss");
</style>